package com.blog.cmrpersonalblog.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 限流配置类
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "rate-limit")
public class RateLimitConfig {
    
    /**
     * 是否启用限流
     */
    private Boolean enabled = true;
    
    /**
     * Redis key前缀
     */
    private String keyPrefix = "rate_limit:";
    
    /**
     * 默认限流配置
     */
    private LimitRule defaultRule = new LimitRule();
    
    /**
     * 自定义限流配置
     */
    private Map<String, LimitRule> custom = new HashMap<>();
    
    /**
     * 限流规则
     */
    @Data
    public static class LimitRule {
        /**
         * 时间窗口（秒）
         */
        private Integer time = 60;
        
        /**
         * 允许请求次数
         */
        private Integer count = 100;
    }
    
    /**
     * 获取自定义限流规则
     */
    public LimitRule getCustomRule(String key) {
        return custom.get(key);
    }
}

